package com.prs.crm.action.oa;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Article;
import com.prs.crm.domain.oa.ArticleType;
import com.prs.crm.domain.oa.HomePage;
import com.prs.crm.domain.oa.HomePageType;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.oa.ArticleService;
import com.prs.crm.service.oa.ArticleTypeService;
import com.prs.crm.service.oa.HomePageService;
import com.prs.crm.service.oa.HomePageTypeService;

public class HomePageAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private HomePage homePage;
	private HomePageService homePageService;
	private HomePageType homePageType;
	private HomePageTypeService homePageTypeService;
	private List<HomePage> pages;
	private List<HomePageType> types;
	private Map<HomePageType, List<HomePage>> map;
	private Integer[] typeIds;
	private String admin;
	private String browse;
	private String post;
	private String dept;
	private DeptService deptService;
	private Article article;
	private ArticleService articleService;
	private ArticleType articleType;
	private ArticleTypeService articleTypeService;
	private List<ArticleType> articleTypes;
	private File upload;
	private String fileName;
	private List<Article> articles;
	private Map<ArticleType,List<Article>> mapar;
	private String num;
	
	@Action("listHomePageAndType")
	public String listHomePageAndType() {
		Person currerPerson=AppContext.getAccountContext().currentUser();
		List<HomePage> hp=this.getHomePageService().getAllHomePage();
		for (HomePage hps : hp) {
			System.out.println(hps.getId());
			if (currerPerson.getId().equals(hps.getRecorder().getId())) {
				hps.setManagerState(currerPerson.getId()+":1");
				hps.setShowState(currerPerson.getId()+":1");
				hps.setPostState(currerPerson.getId()+":1");
				this.getHomePageService().submit(hps);
				
			}else{
				if (hps.getAdminId()==null) {
					hps.setManagerState(":1");
					hps.setShowState(":1");
					hps.setPostState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] perId=hps.getAdminId().split(",");
					for (String persId : perId) {
						if (String.valueOf(currerPerson.getId()).equals(persId)) {
							hps.setManagerState(currerPerson.getId()+":1");
							hps.setShowState(currerPerson.getId()+":1");
							hps.setPostState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setManagerState(":1");
							hps.setShowState(":1");
							hps.setPostState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
				if (hps.getBrowseId()==null) {
					hps.setShowState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] bperId=hps.getBrowseId().split(",");
					for (String bpersId : bperId) {
						if (String.valueOf(currerPerson.getId()).equals(bpersId+"")) {
							hps.setShowState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setShowState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
				if (hps.getPostId()==null) {
					hps.setPostState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] pperId=hps.getPostId().split(",");
					for (String ppersId : pperId) {
						if (String.valueOf(currerPerson.getId()).equals(ppersId)) {
							hps.setPostState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setPostState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
			}		
		}
		
		this.types=this.homePageTypeService.getallType();
		this.map=new HashMap<HomePageType, List<HomePage>>();
		if(types!=null){
		for(int i=0;i<types.size();i++){
			HomePageType type=types.get(i);
			List<HomePage> p=this.homePageService.getPageByType(type);
			map.put(type, p);
		}
	}
		return SUCCESS;
	}
	@Action("listDeptHomePage")
	public String listDeptHomePage() {
		Person currerPerson=AppContext.getAccountContext().currentUser();
		List<HomePage> hp=this.getHomePageService().getAllHomePage();
		for (HomePage hps : hp) {
			if (currerPerson.getId().equals(hps.getRecorder().getId())) {
				hps.setManagerState(currerPerson.getId()+":1");
				hps.setShowState(currerPerson.getId()+":1");
				hps.setPostState(currerPerson.getId()+":1");
				this.getHomePageService().submit(hps);
			}else{
				if (hps.getAdminId()==null) {
					hps.setManagerState(":1");
					hps.setShowState(":1");
					hps.setPostState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] perId=hps.getAdminId().split(",");
					for (String persId : perId) {
						if (String.valueOf(currerPerson.getId()).equals(persId)) {
							hps.setManagerState(currerPerson.getId()+":1");
							hps.setShowState(currerPerson.getId()+":1");
							hps.setPostState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setManagerState(":1");
							hps.setShowState(":1");
							hps.setPostState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
				if (hps.getBrowseId()==null) {
					hps.setShowState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] bperId=hps.getBrowseId().split(",");
					for (String bpersId : bperId) {
						if (String.valueOf(currerPerson.getId()).equals(bpersId)) {
							hps.setShowState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setShowState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
				if (hps.getPostId()==null) {
					hps.setPostState(":1");
					this.getHomePageService().submit(hps);
				}else{
					String[] pperId=hps.getPostId().split(",");
					for (String ppersId : pperId) {
						if (String.valueOf(currerPerson.getId()).equals(ppersId)) {
							hps.setPostState(currerPerson.getId()+":1");
							this.getHomePageService().submit(hps);
							break;
						}else{
							hps.setPostState(":1");
							this.getHomePageService().submit(hps);
						}
					}
				}
			}	
		}
		
		types=this.homePageTypeService.getallType();
		map=new HashMap<HomePageType, List<HomePage>>();
		if(types!=null){
		for(int i=0;i<types.size();i++){
			HomePageType type=types.get(i);
			List<HomePage> p=this.homePageService.getPageByType(type);
			map.put(type, p);
		}
	}
		this.articles=this.articleService.getArticle();
		return SUCCESS;
	}
	@Action("viewHomePage")
	public String viewHomePage() {
		articles=new ArrayList<Article>();
		articleTypes=this.homePage.getArticleTypes();
		this.mapar=new HashMap<ArticleType, List<Article>>();
		for(int i=0;i<articleTypes.size();i++){
			ArticleType art=articleTypes.get(i);
			mapar.put(art, art.getArticles());
			List<Article> list=this.articleService.getArticleByType(art);
			if(list!=null){
				for(int a=0;a<list.size();a++){
					Article articl=list.get(a);
					this.articles.add(articl);
				}
			}
		}
		if(articles==null){
			this.num="0";
		}else{
			this.num=String.valueOf(this.articles.size());
		}
		return SUCCESS;
	}
	@Action("viewArticle")
	public String viewArticle() {
		
		return SUCCESS;
	}
	@Action("listHomePageType")
	public String listHomePageType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				HomePageType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.homePageTypeService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listArticleType")
	public String listArticleType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ArticleType.class);
		if (homePage!=null) {
			criteria.add(Restrictions.eq("page", homePage));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.homePageTypeService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editHomePageType")
	public String editHomePageType() {
		if(typeIds!=null){
			this.homePageType=this.homePageTypeService.get(typeIds[0]);
		}
		return SUCCESS;
	}
	@Action("editArticleType")
	public String editArticleType() {
		if(typeIds!=null){
			this.articleType=this.articleTypeService.get(typeIds[0]);
		}
		
		return SUCCESS;
	}
	@Action("editHomePage")
	public String editHomePage() {
		this.types=this.homePageTypeService.getallType();
		return SUCCESS;
	}
	@Action("editArticle")
	public String editArticle() {
		this.articleTypes=this.articleTypeService.getAllType(homePage);
		if (articleTypes==null) {
			this.addActionError("请先创建文章类别再创建文章！");
			return ERROR;
		}
		return SUCCESS;
	}
	@Action(value = "saveHomePageType", results = {
			@Result(name = "success", type = "redirect", location = "listHomePageType"),
			@Result(name = "error", type = "chain", location = "editHomePageType") })
	public String saveHomePageType() {
		if(this.homePageType.getId()==null){
			this.homePageTypeService.save(homePageType);
		}else{
			this.homePageTypeService.submit(homePageType);
		}
		return SUCCESS;
	}
	@Action(value = "saveArticleType", results = {
			@Result(name = "success", type = "redirect", location = "listArticleType?homePage=${homePage.getId()}"),
			@Result(name = "error", type = "chain", location = "editArticleType?homePage=${homePage.getId()}") })
	public String saveArticleType() {
		articleType.setPage(homePage);
		if(this.articleType.getId()==null){
			this.articleTypeService.save(articleType);
		}else{
			this.articleTypeService.submit(articleType);
		}
		return SUCCESS;
	}
	@Action(value = "saveArticle", results = {
			@Result(name = "success", type = "redirect", location = "listHomePageAndType"),
			@Result(name = "error", type = "chain", location = "editArticle") })
	public String saveArticle() {
		if(upload!=null){
			this.article.setAttach(FileUtil.getInstance().createAttach(upload, fileName));
		}
		if(this.article.getId()==null){
			this.articleService.save(article);
		}else{
			this.articleService.submit(article);
		}
		return SUCCESS;
	}
	@Action(value = "removeArticleType", results = {
			@Result(name = "success", type = "redirect", location = "listArticleType?homePage=${homePage.getId()}"),
			@Result(name = "error", type = "chain", location = "listArticleType?homePage=${homePage.getId()}") })
	public String removeArticleType() {
		this.articleType=this.articleTypeService.get(typeIds[0]);
		if(articleType.getArticles()!=null){
			for(int i=0;i<articleType.getArticles().size();i++){
				Article a=articleType.getArticles().get(i);
				this.articleService.remove(a);
			}
		}
		this.articleTypeService.remove(articleType);
		return SUCCESS;
	}
	@Action(value = "deleteHomePage", results = {
			@Result(name = "success", type = "redirect", location = "listHomePageAndType"),
			@Result(name = "error", type = "chain", location = "listHomePageAndType") })
	public String deleteHomePage() {
//		List<ArticleType> ty=this.homePage.getArticleTypes();
//		if(ty!=null){
//		for(int i=0;i<ty.size();i++){
//			ArticleType at=ty.get(i);
//			List<Article> ar=at.getArticles();
//			if(ar!=null){
//				for(int b=0;b<ar.size();b++){
//					Article a=ar.get(b);
//					System.out.println(a.getTitle());
//					this.articleService.remove(a);
//				}
//			}
//			this.articleTypeService.remove(at);
//		}
//		}
		for (int i = 0; i < homePage.getArticleTypes().size(); i++) {
			ArticleType type=homePage.getArticleTypes().get(i);
			for (int j = 0; j < type.getArticles().size();j++) {
				Article ar=type.getArticles().get(j);
				this.getArticleService().remove(ar);
			}
			this.getArticleTypeService().remove(type);
		}
		this.homePageService.remove(homePage);
		return SUCCESS;
	}
	@Action(value = "removeHomePageType", results = {
			@Result(name = "success", type = "redirect", location = "listHomePageType"),
			@Result(name = "error", type = "chain", location = "listHomePageType") })
	public String removeHomePageType() {
		this.homePageTypeService.batchRemove(typeIds);
		return SUCCESS;
	}
	@Action(value = "saveHomePage", results = {
			@Result(name = "success", type = "redirect", location = "editHomePage"),
			@Result(name = "error", type = "chain", location = "editHomePage") })
	public String saveHomePage() {
		this.homePage.setAdminId(admin);
		this.homePage.setBrowseId(browse);
		this.homePage.setPostId(post);
		Dept d=this.deptService.get(Integer.parseInt(this.dept));
		this.homePage.setDept(d);
		if(this.homePage.getId()==null){
			this.homePageService.save(homePage);
		}else{
			this.homePageService.submit(homePage);
		}
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public HomePage getHomePage() {
		return homePage;
	}
	public void setHomePage(HomePage homePage) {
		this.homePage = homePage;
	}
	public HomePageService getHomePageService() {
		return homePageService;
	}
	public void setHomePageService(HomePageService homePageService) {
		this.homePageService = homePageService;
	}
	public HomePageType getHomePageType() {
		return homePageType;
	}
	public void setHomePageType(HomePageType homePageType) {
		this.homePageType = homePageType;
	}
	public HomePageTypeService getHomePageTypeService() {
		return homePageTypeService;
	}
	public void setHomePageTypeService(HomePageTypeService homePageTypeService) {
		this.homePageTypeService = homePageTypeService;
	}

	public List<HomePage> getPages() {
		return pages;
	}

	public void setPages(List<HomePage> pages) {
		this.pages = pages;
	}

	public List<HomePageType> getTypes() {
		return types;
	}

	public void setTypes(List<HomePageType> types) {
		this.types = types;
	}

	public Map<HomePageType, List<HomePage>> getMap() {
		return map;
	}

	public void setMap(Map<HomePageType, List<HomePage>> map) {
		this.map = map;
	}
	public Integer[] getTypeIds() {
		return typeIds;
	}
	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}
	public String getAdmin() {
		return admin;
	}
	public void setAdmin(String admin) {
		this.admin = admin;
	}
	public String getBrowse() {
		return browse;
	}
	public void setBrowse(String browse) {
		this.browse = browse;
	}
	public String getPost() {
		return post;
	}
	public void setPost(String post) {
		this.post = post;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public DeptService getDeptService() {
		return deptService;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	public Article getArticle() {
		return article;
	}
	public void setArticle(Article article) {
		this.article = article;
	}
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	public ArticleType getArticleType() {
		return articleType;
	}
	public void setArticleType(ArticleType articleType) {
		this.articleType = articleType;
	}
	public ArticleTypeService getArticleTypeService() {
		return articleTypeService;
	}
	public void setArticleTypeService(ArticleTypeService articleTypeService) {
		this.articleTypeService = articleTypeService;
	}
	public List<ArticleType> getArticleTypes() {
		return articleTypes;
	}
	public void setArticleTypes(List<ArticleType> articleTypes) {
		this.articleTypes = articleTypes;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public List<Article> getArticles() {
		return articles;
	}
	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}
	public Map<ArticleType, List<Article>> getMapar() {
		return mapar;
	}
	public void setMapar(Map<ArticleType, List<Article>> mapar) {
		this.mapar = mapar;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	
}
